package com.four.controller;
/*
 * @author 王玉柱
 * @date 2021年10月24日 19:36
 */

import com.four.pojo.*;
import com.four.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class MemberController {
    @Autowired
    @Qualifier("memberService")
    public IMemberService iMemberService;

    @RequestMapping("/selectAllMemberController")
    public String selectAll(Model model) {
        List<OurMember> ourMembers = iMemberService.selectAll();
        model.addAttribute("ourMembers", ourMembers);
        return "user_List.jsp";
    }

    @RequestMapping("/selectOneMemberController")
    public String selectOne(Model model, int id) {
        Member member = iMemberService.selectOne(id);
        model.addAttribute("member", member);
        List<MemberCar> memberCars = iMemberService.selectOne1(id);
        model.addAttribute("memberCars", memberCars);
        List<Card> memberCards = iMemberService.selectOne2(id);
        model.addAttribute("memberCards", memberCards);
        List<Gift> memberGifts = iMemberService.selectOne3(id);
        model.addAttribute("memberGifts", memberGifts);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.set("name", member.getName());
        jedis.set("phone", member.getPhone());
        jedis.set("mid", String.valueOf(member.getMid()));
        jedis.close();
        return "user_Detail.jsp";
    }

    @RequestMapping("/selectOneGiftController")
    public String selectOneGift(Model model, int bixId) {
        List<GiftDetails> giftDetails = iMemberService.selectOneGift(bixId);
        model.addAttribute("giftDetails", giftDetails);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        model.addAttribute("mid", jedis.get("mid"));
        model.addAttribute("phone", jedis.get("phone"));
        model.addAttribute("name", jedis.get("name"));
        jedis.close();
        return "user_Package.jsp";
    }

    @RequestMapping("/selectOneMemberOrderController")
    public String selectOneMemberOrder(Model model, int id) {
        List<Orders> orders = iMemberService.selectOneMemberOrder(id);
        model.addAttribute("all", orders);
        return "order_List.jsp";
    }

    @PostMapping("/addOneMemberController")
    public String addOneMember(AddMember addMember) {
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        addMember.setStore(jedis.get("门店"));
        int mid = iMemberService.addOneMember(addMember);
        addMember.setMid(mid);
        int i = iMemberService.addOneMemberCar(addMember);
        return "redirect:/selectAllMemberController";
    }

    @PostMapping("/findOneMemberController")
    public String findOneMember(Model model, String parameter) {
        if (parameter.equals("")) {
            model.addAttribute("err", "请输入手机号/车牌号/姓名");
        } else {
            Member member = iMemberService.findOneMember(parameter);
            model.addAttribute("member", member);
            List<MemberCar> memberCars = iMemberService.selectOne1(member.getMid());
            model.addAttribute("memberCars", memberCars);
            List<Card> memberCards = iMemberService.selectOne2(member.getMid());
            model.addAttribute("memberCards", memberCards);
            List<Gift> memberGifts = iMemberService.selectOne3(member.getMid());
            model.addAttribute("memberGifts", memberGifts);
            List<Orders> orders = iMemberService.findOneMemberOrders(member.getMid());
            model.addAttribute("orders", orders);
            List<Iintegrals> iintegrals = iMemberService.findOneMemberIintegrals(member.getMid());
            model.addAttribute("iintegrals", iintegrals);
            Jedis jedis = new Jedis("192.168.43.95", 6379);
            jedis.set("mid", String.valueOf(member.getMid()));
            jedis.set("phone", member.getPhone());
            jedis.set("name", member.getName());
            jedis.close();
        }

        return "user_Info.jsp";
    }

    @PostMapping("/updateOneMemberController")
    public String updateOneMemberController(Model model, Member member) {
        int i = iMemberService.updateOneMemberController(member);
        return this.findOneMember(model, member.getC_number());
    }

    @RequestMapping("/gouser_InfoController")
    public String toAddCarController(Model model) {
        model.addAttribute("member", null);
        return "user_Info.jsp";
    }

    @PostMapping("/add_CarController")
    public String add_Car(Model model, MemberCar car) {
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        car.setMid(Integer.parseInt(jedis.get("mid")));
        int i = iMemberService.add_Car(car);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }

    @RequestMapping("/toEdit_CarController")
    public String toEdit_Car(Model model,int cid) {
        MemberCar memberCar=iMemberService.toEdit_Car(cid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        model.addAttribute("memberCar",memberCar);
        model.addAttribute("date",sdf.format(memberCar.getCar_expire()));
        return "edit_Car.jsp";
    }
    @PostMapping("/edit_CarController")
    public String edit_Car(Model model,MemberCar memberCar) {
        int i=iMemberService.edit_Car(memberCar);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/del_CarController")
    public String del_Car(Model model,int cid) {
        int i=iMemberService.del_Car(cid);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/toadd_PcardController")
    public String toadd_Pcard(Model model,String id) {
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        List<Staff> staff=iMemberService.selectOne5();
        model.addAttribute("staff",staff);
        model.addAttribute("phone",jedis.get("phone"));
        jedis.close();
        return "add_Pcard.jsp";
    }
    @PostMapping("/add_PcardController")
    public String add_Pcard(Model model,Card card) {
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        Date date = new Date();
        card.setcTime(date);
        card.setState("可使用");
        card.setStore(jedis.get("店铺")+"");
        card.setName(jedis.get("name"));
        card.setStaff_id(Integer.valueOf(jedis.get("mid")));
        int i=iMemberService.add_Pcard(card);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/topcard_UpgradeController")
    public String topcard_Upgrade(Model model,int id){
        Card card=iMemberService.selectOne6(id);
        model.addAttribute("card",card);
        List<Staff> staff = iMemberService.selectOne5();
        model.addAttribute("staff",staff);
        return "pcard_Upgrade.jsp";
    }
    @PostMapping("/pcard_UpgradeController")
    public String pcard_Upgrade(Model model,Card card){
        int i=iMemberService.pcard_Upgrade(card);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/topcard_RechargeController")
    public String topcard_Recharge(Model model,int id){
        Card card=iMemberService.selectOne6(id);
        model.addAttribute("card",card);
        List<Staff> staff = iMemberService.selectOne5();
        model.addAttribute("staff",staff);
        return "pcard_Recharge.jsp";
    }
    @PostMapping("/pcard_RechargeController")
    public String pcard_Recharge(Model model,Card card){
        card.setcTime(new Date());
        int i=iMemberService.pcard_Recharge(card);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/pcard_deleteController")
    public String pcard_delete(Model model,int id){
        int i=iMemberService.pcard_delete(id);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/toadd_YcardController")
    public String toadd_Ycard(Model model,int id){
        List<MemberCar> memberCars = iMemberService.selectOne1(id);
        model.addAttribute("MemberCar",memberCars);
        List<Staff> staff = iMemberService.selectOne5();
        model.addAttribute("staff",staff);
        Jedis jedis = new Jedis("192.168.1.51", 6379);
        model.addAttribute("phone",jedis.get("phone"));
        jedis.close();
        return "add_Ycard.jsp";
    }
    @PostMapping("/add_YcardController")
    public String add_Ycard(Model model,Card card){
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        Date date = new Date();
        card.setcTime(date);
        card.setState("可使用");
        card.setStore(jedis.get("店铺")+"");
        card.setName(jedis.get("name"));
        card.setStaff_id(Integer.valueOf(jedis.get("mid")));

        Calendar calendar =Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动

        card.setEndTime(calendar.getTime());
        int i=iMemberService.add_Ycard(card);
        System.out.println(i);
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }
    @RequestMapping("/toadd_SpreeController")
    public String toadd_Spree(Model model,int id){
        List<MemberCar> memberCars = iMemberService.selectOne1(id);
        model.addAttribute("MemberCar",memberCars);
        List<Staff> staff = iMemberService.selectOne5();
        model.addAttribute("staff",staff);
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        model.addAttribute("phone",jedis.get("phone"));
        Date date = new Date();
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
        model.addAttribute("time",calendar.getTime());
        jedis.close();
        return "add_Spree.jsp";
    }
    @PostMapping("/add_SpreeController")
    public String add_Spree(Model model,Gift gift){
        Jedis jedis = new Jedis("192.168.43.95", 6379);
        Date date = new Date();
        gift.setTion_time(date);
        gift.setState("可使用");
        gift.setMname(jedis.get("name"));
        gift.setMid(jedis.get("mid"));
        if (gift.getGiftnum().equals("大礼包一")){
            gift.setPrice("2999");
        }else if (gift.getGiftnum().equals("大礼包二")){
            gift.setPrice("1999");
        }else if (gift.getGiftnum().equals("大礼包三")){
            gift.setPrice("999");
        }
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动

        gift.setGuotime(calendar.getTime());
        int i=iMemberService.add_Spree(gift);
        GiftDetails giftDetails = new GiftDetails();
        int i1;
        giftDetails.setGid(i);
        giftDetails.setMid(jedis.get("mid"));
        giftDetails.setMname(jedis.get("name"));
        giftDetails.setPhone(jedis.get("phone"));
        if (gift.getGiftnum().equals("大礼包一")){
            i1 = iMemberService.add_GiftDetails1(giftDetails);
        }else if (gift.getGiftnum().equals("大礼包二")){
            i1 = iMemberService.add_GiftDetails2(giftDetails);
        }else if (gift.getGiftnum().equals("大礼包三")){
            i1 = iMemberService.add_GiftDetails3(giftDetails);
        }
        jedis.close();
        return this.findOneMember(model, jedis.get("phone"));
    }

}
